using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using TMS.Application.Common.Interface;
using TMS.Application.Utils;
using TMS.Domain.Entity;

namespace TMS.Api.Controllers
{
    [ApiController]
    public class AdministratorsController : ControllerBase
    {
        private readonly IRepository<College> _College;

        private readonly IRepository<Major> _Major;
        private readonly IRepository<Class> _Class;
        private readonly IIdentityService _IdentityService;

        private readonly IRepository<Students> _Students;
        private readonly IRepository<Book> _Book;
        private readonly IRepository<Term> _Term;


        public AdministratorsController(
            IIdentityService IdentityService,
            IRepository<College> College,
            IRepository<Major> Major,
             IRepository<Class> Class,
            IRepository<Students> Students,
            IRepository<Book> Book,
            IRepository<Term> Term

        )
        {
            _Term = Term;
            _Book = Book;
            _Class = Class;
            _Major = Major;
            _College = College;
            _Students = Students;
            _IdentityService = IdentityService;
        }

        [HttpPost("/AddCollege")]
        public async Task<object> AddCollege(College College)
        {
            //添加学院
            if (College.CollegeName != null && College.CollegeName != "")
            {
                try
                {
                    await _College.AddAsync(College);
                    return new
                    {
                        code = 200,
                        msg = "添加成功",
                        data = ""
                    };
                }
                catch (System.Exception)
                {
                    return new
                    {
                        code = 400,
                        msg = "添加失败",
                        data = ""
                    };
                }
            }
            else
            {
                return new
                {
                    code = 400,
                    msg = "添加失败，学院为空",
                    data = ""
                };
            }
        }


        [HttpPost("/AddMajor")]
        public async Task<object> AddMajor(Major Major)
        {
            var College = _College.Table.Where(x => x.Id == Major.CollegeId).ToList();
            //添加专业
            if (College !=null && Major.MajorName != "")
            {
                try
                {
                    await _Major.AddAsync(Major);
                    return new
                    {
                        code = 200,
                        msg = "添加成功",
                        data = ""
                    };
                }
                catch (System.Exception)
                {
                    return new
                    {
                        code = 400,
                        msg = "添加失败",
                        data = ""
                    };
                }
            }
            else
            {
                return new
                {
                    code = 400,
                    msg = "添加失败，专业为空为空或者学院id为错误",
                    data = ""
                };
            }
        }

        [HttpPost("/AddClass")]
        public async Task<object> AddClass(Class Class)
        {
            var Major = _Major.Table.Where(x => x.Id == Class.MajorId).FirstOrDefault();
            //添加班级
            if (Major != null && Class.ClassName != "")
            {
                try
                {
                    await _Class.AddAsync(Class);
                    return new
                    {
                        code = 200,
                        msg = "添加成功",
                        data = ""
                    };
                }
                catch (System.Exception)
                {
                    return new
                    {
                        code = 400,
                        msg = "添加失败",
                        data = ""
                    };
                }
            }
            else
            {
                return new
                {
                    code = 400,
                    msg = "添加失败，班级为空或者专业id为错误",
                    data = ""
                };
            }

        }

        [HttpGet("/displayCollege/{year}")]
        public object displayCollege(int year)
        {

            Method method = new Method(
             _IdentityService,
             _College,
             _Major,
              _Class,
            _Students
             );



            //学院列表数组 内有学院人数和专业数，并且带有专业信息
            List<object> displayCollege = new List<object>();

            //学院列表
            var College = _College.Table.Where(x => x.CreatedAt.Year == year).ToList();

            //专业列表
            var Major = _Major.Table.Where(x => x.CreatedAt.Year == year).ToList();
            //班级列表
            var Class = _Class.Table.Where(x => x.CreatedAt.Year == year).ToList();

            //学生列表信息
            //var StudentList=_Students.Table.ToList();
            var StudentList = method.StudentList().ToList();


            foreach (var item in College)
            {


                //学院的专业
                var Majors = Major.Where(x => x.CollegeId == item.Id).ToList();
                //专业的班级
                // var Major_Class=Class.Where(x=>x.MajorId)
                //学院专业数量
                var MajorsNum = Majors.Count();

                //学院人数
                var CollegePeopleSum = StudentList.Where(x => x.CollegeId == item.Id).ToList().Count();


                List<object> MajorInformation = new List<object>();
                foreach (var items in Majors)
                {
                    //学院专业人数
                    var MajorsPeopleSum = StudentList.Where(x => x.MajorId == items.Id).ToList().Count();
                    MajorInformation.Add(new
                    {
                        items.CollegeId,
                        items.MajorName,
                        MajorsPeopleSum
                    });
                }

                displayCollege.Add(new
                {
                    item.Id,
                    item.CollegeName,
                    MajorsNum,
                    CollegePeopleSum,
                    MajorInformation

                });
            }
            return new
            {
                code = 200,
                msg = "查询完成",
                data = displayCollege
            };
        }


        [HttpGet("/CollegeList")]

        public object CollegeList(int year)
        {
            //如果需要学生的连表的话请使用这个
            Method method = new Method(
                _IdentityService,
                _College,
                _Major,
                 _Class,
               _Students
                );

            //学院的连表
            var CollegeList = method.CollegeList().ToList();

            return new
            {
                code = 200,
                msg = "查询完成",
                data = CollegeList
            };
        }





        [HttpGet("/StudentList")]
        public async Task<object> StudentList(Guid id)
        {
            Method method = new Method(
            _IdentityService,
            _College,
            _Major,
            _Class,
            _Students
            );

            //查找所有的学生
            var StudentList = _Students.Table.ToList();

            //学生学院列表
            var students = method.StudentList().ToList();
            //学院列表
            var College = _College.Table.ToList();

            //专业列表
            var Major = _Major.Table.ToList();
            //班级列表
            var Class = _Class.Table.ToList();

            List<object> list = new List<object>();
            foreach (var item in StudentList)
            {
                var studentLink = students.Where(x => x.Id == item.Id).FirstOrDefault();
                var Student_College = College.Where(x => x.Id == studentLink.CollegeId).FirstOrDefault();
                var Student_Major = Major.Where(x => x.Id == studentLink.MajorId).FirstOrDefault();
                var Student_Class = Class.Where(x => x.Id == studentLink.ClassId).FirstOrDefault();
                list.Add(new
                {
                    CollegeName = Student_College.CollegeName,
                    MajorName = Student_Major.MajorName,
                    ClassName = Student_Class.ClassName,
                    item
                });

            }
            return new
            {
                code = 200,
                msg = "查询完成",
                data = list
            };


        }

        [HttpGet("/StudentList/{id}")]
        public async Task<object> Class_StudentList(Guid id)
        {
            Method method = new Method(
            _IdentityService,
            _College,
            _Major,
            _Class,
            _Students
            );

            //根据班级id查找班级的学生
            var StudentList = _Students.Table.Where(x => x.ClassId == id).ToList();

            //学生学院列表
            var students = method.StudentList().ToList();
            //学院列表
            var College = _College.Table.ToList();

            //专业列表
            var Major = _Major.Table.ToList();
            //班级列表
            var Class = _Class.Table.ToList();

            List<object> list = new List<object>();
            foreach (var item in StudentList)
            {
                var studentLink = students.Where(x => x.Id == item.Id).FirstOrDefault();
                var Student_College = College.Where(x => x.Id == studentLink.CollegeId).FirstOrDefault();
                var Student_Major = Major.Where(x => x.Id == studentLink.MajorId).FirstOrDefault();
                var Student_Class = Class.Where(x => x.Id == studentLink.ClassId).FirstOrDefault();
                list.Add(new
                {
                    CollegeName = Student_College.CollegeName,
                    MajorName = Student_Major.MajorName,
                    ClassName = Student_Class.ClassName,
                    item
                });

            }
            return new
            {
                code = 200,
                msg = "查询完成",
                data = list
            };


        }


        [HttpDelete("/DelStudent/{id}")]
        public async Task<object> DelStudent(Guid id)
        {

            try
            {
                var Student = _Students.Table.Where(x => x.Id == id).FirstOrDefault();
                if (Student.Id != null)
                {
                    await _Students.DeleteAsync(id);
                    return new
                    {
                        code = 200,
                        msg = "删除成功",
                        data = ""
                    };
                }
                else
                {
                    return new
                    {
                        code = 400,
                        msg = "删除失败,id为找到",
                        data = ""
                    };
                }

            }
            catch (System.Exception)
            {

                return new
                {
                    code = 400,
                    msg = "删除失败",
                    data = ""
                };
            }

        }

        [HttpPost("/UpdateStudent/{id}")]
        public async Task<object> UpdateStudent(Students Student)
        {
            //修改学生未测试
            try
            {
                var Students = _Students.Table.Where(x => x.Id == Student.Id).FirstOrDefault();
                Students.StudentId = Student.StudentId;
                Students.ClassId = Student.ClassId;
                Students.StudentName = Student.StudentName;
                Students.Sex = Student.Sex;
                Students.IdentityNumber = Student.IdentityNumber;
                Students.PhoneNumber = Student.PhoneNumber;
                Students.ChattingNumber = Student.ChattingNumber;
                Students.QQNumber = Student.QQNumber;
                Students.Address = Student.Address;
                Students.FatherName = Student.FatherName;
                Students.FatherPhone = Student.FatherPhone;
                Students.MontherName = Student.MontherName;
                Students.MontherPhone = Student.MontherPhone;
                Students.ClassId = Student.ClassId;
                Students.DormId = Student.DormId;


                await _Students.UpdateAsync(Students);
                return new
                {
                    code = 200,
                    msg = "修改成功",
                    data = ""
                };
            }
            catch (System.Exception)
            {

                return new
                {
                    code = 400,
                    msg = "修改失败",
                    data = ""
                };
            }

        }


        [HttpDelete("/DelCollege/{id}")]
        public async Task<object> DelCollege(Guid id)
        {
            //删除学院并且把专业删除
            try
            {
                var Major = _Major.Table.Where(x => x.CollegeId == id).ToList();
                foreach (var item in Major)
                {
                    var Class = _Class.Table.Where(x => x.MajorId == item.Id).ToList();
                    _Class.DeleteBulkAsync(Class, true);

                }
                _Major.DeleteBulkAsync(Major, true);
                var CollegeList = _College.Table.ToList();
                var College = _College.Table.Where(x => x.Id == id).FirstOrDefault();
                if (College.Id != null)
                {
                    await _College.DeleteAsync(id, true);
                    return new
                    {
                        code = 200,
                        msg = "删除成功",
                        data = ""
                    };
                }
                else
                {
                    return new
                    {
                        code = 400,
                        msg = "删除失败,id为找到",
                        data = ""
                    };
                }

            }
            catch (System.Exception)
            {

                return new
                {
                    code = 400,
                    msg = "删除失败",
                    data = ""
                };
            }
        }

        [HttpPost("/AddBook")]
        public async Task<object> AddBook(Book book)
        {
            try
            {
                var Book = _Major.Table.Where(x => x.Id == book.MajorId).FirstOrDefault();
                if (Book != null && book.BookName != "" && book.Price != 0 && book.Count != "" && book.Supplier != "" && book.TermId != null)
                {

                    await _Book.AddAsync(book);
                    return new
                    {
                        code = 200,
                        msg = "添加成功",
                        data = ""
                    };
                }
                else
                {
                    return new
                    {
                        code = 400,
                        msg = "添加失败",
                        data = ""
                    };
                }
            }
            catch (System.Exception)
            {

                return new
                {
                    code = 500,
                    msg = "添加错误",
                    data = ""
                };
            }

        }

        [HttpGet("/displayBook")]
        public async Task<object> displayBook()
        {
            try
            {



                //教材表
                var books = _Book.Table.ToList();
                //专业表
                var Majors = _Major.Table.ToList();
                //学院表
                var Colleges = _College.Table.ToList();

                //学期表
                var TermList = _Term.Table.ToList();
                List<Object> BookList = new List<object>();
                foreach (var item in books)
                {

                    var Major = Majors.Where(x => x.Id == item.MajorId).FirstOrDefault();
                    var Term = TermList.Where(x => x.Id == item.TermId).FirstOrDefault();
                    if (Term == null)
                    {
                        await _Book.DeleteAsync(item.Id, true);
                    }
                    else
                    {
                        if (Major != null)
                        {
                            var College = Colleges.Where(x => x.Id == Major.CollegeId).FirstOrDefault();

                            BookList.Add(new
                            {
                                item.Id,
                                item.BookName,
                                item.Count,
                                item.Price,
                                item.Supplier,
                                MajorId = Major.Id,
                                Major.MajorName,
                                College.CollegeName,
                                CollegeId = College.Id,
                                item.TermId,
                                Term = Term.TermName
                            });
                        }
                        else
                        {
                            // 如果查找不到专业id就把这教程删除
                            await _Book.DeleteAsync(item.Id, true);
                        }
                    }

                }

                return new
                {
                    code = 200,
                    msg = "请求成功",
                    data = BookList
                };
            }
            catch (System.Exception)
            {

                return new
                {
                    code = 500,
                    msg = "请求错误",
                    data = ""
                };
            }


        }


        [HttpGet("/displayTerm")]

        public async Task<object> displayTerm()
        {
            try
            {
                var TermList = _Term.Table.ToList();
                return new
                {
                    code = 200,
                    msg = "请求成功",
                    data = TermList
                };
            }
            catch (System.Exception)
            {
                return new
                {
                    code = 500,
                    msg = "请求错误",
                    data = ""
                };
            }

        }

        [HttpDelete("/DelTerm/{id}")]
        public async Task<object> DelTerm(Guid id)
        {
            try
            {
                var Book = _Book.Table.Where(x => x.Id == id).FirstOrDefault();
                if (Book != null)
                {
                    await _Book.DeleteAsync(id, true);
                    return new
                    {
                        code = 200,
                        msg = "删除成功",
                        data = ""
                    };
                }
                else
                {
                    return new
                    {
                        code = 400,
                        msg = "删除失败,没有改该教材",
                        data = ""
                    };
                }

            }
            catch (System.Exception)
            {
                return new
                {
                    code = 500,
                    msg = "删除错误",
                    data = ""
                };
            }
        }

        [HttpDelete("/DelClass/{id}")]

        public async Task<object> DelClass(Guid id)
        {
            try
            {
                var Class = _Class.Table.Where(x => x.Id == id).FirstOrDefault();
                if (Class != null)
                {
                    await _Class.DeleteAsync(id, true);
                    return new
                    {
                        code = 200,
                        msg = "删除成功",
                        data = ""
                    };
                }
                else
                {
                    return new
                    {
                        code = 400,
                        msg = "删除失败,没有改班级",
                        data = ""
                    };
                }

            }
            catch (System.Exception)
            {
                return new
                {
                    code = 500,
                    msg = "删除错误",
                    data = ""
                };
            }
        }

        
        [Authorize]
        [HttpPost("/token")]
        public async Task<object> token()
        {
            return new
            {
                code = 200,
                msg = "验证成功token未过期",
                data = ""
            };
        }




















    }

}